package com.xmg.boot.demo.bootcommon.utils;

import com.xmg.boot.demo.bootcommon.utils.enums.StatusCode;
import lombok.Data;
import lombok.ToString;

import java.io.Serializable;

/**
 * @author: ma
 * @date: 2023-12-04
 * @since: 结果返回类
 */
@Data
@ToString

public class Result<T> implements Serializable {
    private String code;//返回码
    private String msg;//返回消息
    private T data;//返回数据

    public Result(String code, String message, T data) {
        this.code = code;
        this.msg = message;
        this.data = (T) data;
    }

    public Result( String code, String message) {
        this.code = code;
        this.msg = message;
    }

    public static Result success(){
        return new Result(StatusCode.OK.getCode(), StatusCode.OK.getValue());
    }

    public static <T> Result<T> success(T data){
        return new Result<>(StatusCode.OK.getCode(), StatusCode.OK.getValue(),data);
    }

    public static <T> Result<T> success(String message, T data){
        return new Result<>(StatusCode.OK.getCode(),message,data);
    }

    public static <T> Result<T> fail(String message){
        return new Result<>(StatusCode.ERROR.getCode(),message);
    }

    public static <T> Result<T> fail(String code, String message) {
        return new Result<>(code, message);
    }

    public static void main(String[] args) {
        System.out.println(Result.success());
        System.out.println(Result.success("55"));
        System.out.println(Result.success("jlksfkd",200));
        System.out.println(Result.fail("sfsdf"));
        System.out.println(Result.fail("5959","sdfsdfsf"));
    }
}
